import pandas as pd
import math
import numpy as np

from Core.Config import Config
config = Config()
database = config.DataBase()
database.creatIndex("Label", "Tag", "key")
xls_file = pd.ExcelFile(r'C:\Users\zhujunheng\Desktop\tag.xlsx')
sheet_names = xls_file.sheet_names
df_all = pd.DataFrame()
for i in sheet_names:
    df = pd.read_excel(r'C:\Users\zhujunheng\Desktop\tag.xlsx', header = None, sheet_name=i, skiprows=0, index=False, encoding='utf8')
    tag = i
    if tag == "未分类":
        continue
    length = df._info_axis._data.size
    no_of_subs = int(math.ceil(length / 2))
    for j in range(no_of_subs):
        for i in range(1, len(df)):
            term = df.iloc[i, j * 2]
            if term is np.nan:
                # print(term)
                continue
            if no_of_subs >1:
                subtag = df.iloc[0, j * 2]


            followersDoc = {}

            followersDoc["term"] = term
            followersDoc["class"] = tag
            followersDoc["source"] = "liangbing"
            if no_of_subs > 1:
                followersDoc["subclass"] = subtag

            followersDoc["Key"] = term
            print(followersDoc)
            database.upsert("Label", "Tag", {"Key": followersDoc["Key"]}, followersDoc)





